﻿Imports System.Drawing.Printing
Imports System.IO

Public Class FormularioConfirmarTicket
    Dim tamanhoTicket As New PaperSize("ticket", 1, 20)

    Private Sub ticket_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles ticket.PrintPage
        e.PageSettings.PaperSize = tamanhoTicket

        Dim ejex = 80, ejey = 130
        Dim fuente = New Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Point)
        Dim fuente2 = New Font("Lucida Console", 6, FontStyle.Regular, GraphicsUnit.Point)
        Dim color As Brush = Brushes.Black

        Dim detalle As String = ""
        Dim precio As Integer = 0
        Dim ejeyTrasLista As Integer

        With e.Graphics
            .DrawString("---------------------------------------------------", Font, color, 60, 20)
            .DrawString("       eleeme", fuente, color, 60, 40)
            .DrawString(" Guzmán el Bueno, 19. 28003 Madrid", Font, color, 60, 70)
            .DrawString("---------------------------------------------------", Font, color, 60, 80)
            .DrawString("Fecha emisión: " & Now(), Font, color, 60, 100)
            '.DrawString("---------------------------------------------------", Font, color, 60, 110)
            For i = 0 To FormularioTratamientos.lbTicket.Items.Count - 1
                detalle = FormularioTratamientos.lbTicket.Items(i)
                precio = FormularioTratamientos.lbPrecio.Items(i)
                If detalle.Length > 30 Then
                    detalle = detalle.Substring(0, 29)
                Else
                    While detalle.Length < 30
                        detalle &= "."
                    End While
                End If
                .DrawString(detalle & " " & RSet(precio, 3), fuente2, Brushes.Black, ejex, ejey + (i * 20))
                ejeyTrasLista = ejey + (i * 20)
            Next
            ejeyTrasLista += 20
            .DrawString("IMPORTE TOTAL................." & RSet(FormularioTratamientos.tbTotal.Text, 5), fuente2, Brushes.Black, ejex, ejeyTrasLista)
        End With
        e.Graphics.DrawString("Le atendió: " & FormularioTratamientos.labEmpleado.Text, Font, color, 60, ejeyTrasLista + 20)
        e.Graphics.DrawString("GRACIAS POR SU VISITA.", Font, color, 60, ejeyTrasLista + 50)


    End Sub
    Private Sub ButImprimirTicket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butImprimirTicket.Click
        ''si no se han impreso los bonos, pedir

        If (pdTicket.ShowDialog = Windows.Forms.DialogResult.OK) Then
            ticket.Print()
            'borramos el ticket
            FormularioTratamientos.lbPrecio.Items.Clear()
            FormularioTratamientos.lbTicket.Items.Clear()
            'AÑADIR A CAJA
            anhadirCompra()
            Me.Close()
        End If
    End Sub

    Private Sub FormularioConfirmarTicket_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub anhadirCompra()
        Dim recaudacionDia, recaudacionMes, recaudacionAnho As Integer
        Dim diaRuta As String = "c:/eleemeTPV/caja/" & Today.Day & Today.Month & Today.Year & ".txt"
        Dim mesRuta As String = "c:/eleemeTPV/caja/" & Today.Year & Today.Month & ".txt"
        Dim anhoRuta As String = "c:/eleemeTPV/caja/" & Today.Year & ".txt"
        Dim cajaDia As FileStream = New FileStream(diaRuta, FileMode.OpenOrCreate, FileAccess.Read)
        Dim cajaMes As FileStream = New FileStream(mesRuta, FileMode.OpenOrCreate, FileAccess.Read)
        Dim cajaAnho As FileStream = New FileStream(anhoRuta, FileMode.OpenOrCreate, FileAccess.Read)
        Dim sr As New StreamReader(cajaDia)
        recaudacionDia = Integer.Parse(sr.ReadLine)
        sr.Close()
        sr = New StreamReader(cajaMes)
        recaudacionMes = Integer.Parse(sr.ReadLine)
        sr.Close()
        sr = New StreamReader(cajaAnho)
        recaudacionMes = Integer.Parse(sr.ReadLine)
        sr.Close()
        cajaDia.Close()
        cajaMes.Close()
        cajaAnho.Close()


        cajaDia = New FileStream(diaRuta, FileMode.Create, FileAccess.Write)
        cajaMes = New FileStream(mesRuta, FileMode.Create, FileAccess.Write)
        cajaAnho = New FileStream(anhoRuta, FileMode.Create, FileAccess.Write)
        Dim sw As New StreamWriter(cajaDia)
        recaudacionDia += Integer.Parse(FormularioTratamientos.tbTotal.Text)
        sw.Write(recaudacionDia)
        sw.Close()
        sw = New StreamWriter(cajaMes)
        recaudacionMes += Integer.Parse(FormularioTratamientos.tbTotal.Text)
        sw.Write(recaudacionMes)
        sw.Close()
        sw = New StreamWriter(cajaAnho)
        recaudacionAnho += Integer.Parse(FormularioTratamientos.tbTotal.Text)
        sw.Write(recaudacionAnho)
        sw.Close()
        cajaDia.Close()
        cajaMes.Close()
        cajaAnho.Close()

        ''borramos el campo total
        FormularioTratamientos.tbTotal.Text = 0
    End Sub
End Class
